﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;

namespace Program09
{
    class Program
    {
        //随机初始化100个员工,随机生成一些中文名字
        public static List<Person> GetPersons()
        {
            var list=new List<Person>();
            //100个员工
            for (var i = 0; i < 101; i++)
            {
                var fName = "";
                var lName = "";
                GetRandomChineseName(ref fName,ref  lName);
                var p = new Person()
                {
                    EmployeeNo = "Lzzy" + (i + 1).ToString("001"),
                    FirstName = fName,
                    LastName = lName,
                    Phone = "1388888" + i.ToString("0000")
                };
                using (FileStream fsWrite = new FileStream("person.db", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    //读对象内容，转换为二进制，序列化
                    var buffer = new BinaryFormatter();
                    buffer.Serialize(fsWrite,p);
                }
                
                list.Add(p);
                Thread.Sleep(1);
            }
            Console.WriteLine("序列化成功，存储成功");
            return list;
        }
        /// <summary>
        /// 随机生成中文名字
        /// </summary>
        /// <param name="fName">姓</param>
        /// <param name="lName">名</param>
        public static void GetRandomChineseName(ref string fName, ref string lName)
        {
            string[] fNames = new string[]
            {
                "白","毕","卞","蔡","曹","岑","常","车","陈","成" ,"程","池","邓","丁","范","方","樊","费","冯","符"
                ,"傅","甘","高","葛","龚","古","关","郭","韩","何" ,"贺","洪","侯","胡","华","黄","霍","姬","简","江"
                ,"姜","蒋","金","康","柯","孔","赖","郎","乐","雷" ,"黎","李","连","廉","梁","廖","林","凌","刘","柳"
                ,"龙","卢","鲁","陆","路","吕","罗","骆","马","梅" ,"孟","莫","母","穆","倪","宁","欧","区","潘","彭"
                ,"蒲","皮","齐","戚","钱","强","秦","丘","邱","饶" ,"任","沈","盛","施","石","时","史","司徒","苏","孙"
                ,"谭","汤","唐","陶","田","童","涂","王","危","韦" ,"卫","魏","温","文","翁","巫","邬","吴","伍","武"
                ,"席","夏","萧","谢","辛","邢","徐","许","薛","严" ,"颜","杨","叶","易","殷","尤","于","余","俞","虞"
                ,"元","袁","岳","云","曾","詹","张","章","赵","郑" ,"钟","周","邹","朱","褚","庄","卓"
            };
            string lNames = "凌之丹梅凝易梦夏如蕾孤竹丹晴尔梅友春思天梦白醉柔问雁听莲觅烟怀白雨薇海风半丹念兰" +
                            "代蓉幼亦依绿凌冬绮真盼海飞青冬香映桃亦真慕玉听琴翠雁晓槐语文念阳春芙飞凝醉竹凌菱" +
                            "雅荷绮春山薇雅夏涵霜香柔语雪恨冬寒春飞莲平念寄海沛菱雨香亦菡慕香静双半香凝阳问秋" +
                            "梦薇尔翠天凡念儿小海碧松如天语巧雁雪雪柏惜彤幼卉巧晴友天幻文诗雁孤珊乐丝采秋书梦";

            var rnd=new Random(DateTime.Now.Millisecond);
            fName = fNames[rnd.Next(0, fNames.Length - 1)];

            lName = lNames.Substring(rnd.Next(0, lNames.Length - 1), 1) +
                    lNames.Substring(rnd.Next(0, lNames.Length - 1), 1);
        }
        static void Main(string[] args)
        {
            //var firstName = "";
            //var lastName = "";
            //GetRandomChineseName(ref firstName,ref lastName);
            //Console.WriteLine(firstName + lastName);

            //GetRandomChineseName(ref firstName, ref lastName);
            //Console.WriteLine(firstName + lastName);

            //GetRandomChineseName(ref firstName, ref lastName);
            //Console.WriteLine(firstName + lastName);
            //var p=new Person();
            //Console.WriteLine(p.ID);


            //var list = GetPersons();
            //foreach (var p in list)
            //{
            //    Console.WriteLine("工号:{0} 姓名:{1} 性别:{2} 生日:{3} 手机:{4} 唯一标识:{5}",p.EmployeeNo
            //    ,p.FullName,p.Sex?"男":"女",p.BirthDay.ToString("yyyy年M月d日"),p.Phone,p.ID);
            //}
            var list = GetPersons();
            Person P;
            foreach (var p in list)
            {
                using (FileStream fsRead = new FileStream("person.db", FileMode.OpenOrCreate, FileAccess.Read))
                {
                    var buffer = new BinaryFormatter();
                    P = (Person) buffer.Deserialize(fsRead);
                }

                Console.WriteLine("工号:{0} 姓名:{1} 性别:{2} 生日:{3} 手机:{4} 唯一标识:{5}", p.EmployeeNo
                    , p.FullName, p.Sex ? "男" : "女", p.BirthDay.ToString("yyyy年M月d日"), p.Phone, p.ID);

            }

        }
    }
}
